Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[docs] Fix 'property' has no initializer issue in TypeScript property samples #1274

Merged
merged 1 commit into from
Nov 21, 2023

Conversation

AndrewJakubowicz
Copy link
Contributor

Fixes: #1273

Context

When looking at our documentation, the very first TypeScript example shows: https://lit.dev/docs/components/properties/

class MyElement extends LitElement {
  @property()
  name: string;
}

This results in a TypeScript error: Property 'name' has no initializer and is not definitely assigned in the constructor..

This can be globally resolved by relaxing the TypeScript compiler via "strictPropertyInitialization": false, but we should prefer documentation that is as explicit/strict as possible.

We have an example in the playground where it makes sense to not initialize a property. So I've added that syntax to our properties.md docs.

Fix:

class MyElement extends LitElement {
  @property()
  name?: string;
}

This clearly states that the property should be a string, but it can also be undefined if no-one initialized it.

Risk & Tests

This is a low risk content only change – tested by checking out preview URL.

Copy link

A live preview of this PR will be available at the URL(s) below.
The latest URL will be appended to this comment on each push.
Each build takes ~5-10 minutes, and will 404 until finished.

https://pr1274-f194af1---lit-dev-5ftespv5na-uc.a.run.app/

Copy link
Member

@augustjk augustjk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's okay for now.. optional won't be valid once we start using accessor but we can cross that bridge later. Alternative would be providing defaults but I can't think of good ones at the moment.

@AndrewJakubowicz AndrewJakubowicz merged commit ac6771e into main Nov 21, 2023
15 checks passed
@AndrewJakubowicz AndrewJakubowicz deleted the no-initializer-docs branch November 21, 2023 23:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Property 'blockId' has no initializer and is not definitely assigned in the constructor.
2 participants